package webapi

import (
	"gitee.com/rainbowserver_go/config"
	"gitee.com/rainbowserver_go/webapi/rabgame"
	"github.com/Unknwon/goconfig"
	"github.com/gin-gonic/gin"
	"net/http"
	"strings"
	"time"
)

type Router struct {
	whiteList string
}

type BetSort struct {
	Date string `form:"date" json:"date" xml:"date" binding:"-"`
	User string `form:"user" json:"user" xml:"user" binding:"-"`
	Bets int    `form:"bets" json:"bets" xml:"bets" binding:"-"`
}

type CarGameList struct {
	Limit int `form:"limit" json:"limit" xml:"limit" binding:"-"`
}

type CarBetList struct {
	Gameid int64  `form:"gameid" json:"gameid" xml:"gameid" binding:"-"`
	Userid int64  `form:"userid" json:"userid" xml:"userid" binding:"-"`
	User   string `form:"user" json:"user" xml:"userid" binding:"-"`
	Limit  int    `form:"limit" json:"limit" xml:"limit" binding:"-"`
}

type ByGameId struct {
	Gameid int `form:"gameid" json:"gameid" xml:"gameid" binding:"-"`
}

type Raise struct {
	Userid       int64  `form:"userid" json:"userid" xml:"userid" binding:"-"`
	Eos_amount   string `form:"eos_amount" json:"eos_amount" xml:"eos_amount" binding:"-"`
	Token_amount string `form:"token_amount" json:"token_amount" xml:"token_amount" binding:"-"`
	Raise_amount string `form:"raise_amount" json:"raise_amount" xml:"raise_amount" binding:"-"`
	Createtime   int32  `form:"createtime" json:"createtime" xml:"createtime" binding:"-"`
}

type CarGame struct {
	Gameid     int64  `form:"gameid" json:"gameid" xml:"gameid" binding:"-"`
	Game       int8   `form:"game" json:"game" xml:"game" binding:"-"`
	Number     int8   `form:"number" json:"number" xml:"number" binding:"-"`
	Pool       string `form:"pool" json:"pool" xml:"pool"  binding:"-"`
	Peoples    int16  `form:"peoples" json:"peoples" xml:"peoples" binding:"-"`
	Createtime int32  `form:"createtime" json:"createtime" xml:"createtime" binding:"-"`
}

type CarBet struct {
	Id       int64  `form:"id" json:"id" xml:"id" binding:"-"`
	Gameid   int64  `form:"gameid" json:"gameid" xml:"gameid" binding:"-"`
	Userid   int64  `form:"userid" json:"userid" xml:"userid" binding:"-"`
	User     string `form:"user" json:"user" xml:"user" binding:"-"`
	Digital  int8   `form:"digital" json:"digital" xml:"digital"  binding:"-"`
	Quantity string `form:"quantity" json:"quantity" xml:"quantity" binding:"-"`
	Reward   string `form:"reward" json:"reward" xml:"reward" binding:"-"`
	Bettime  int32  `form:"bettime" json:"bettime" xml:"bettime" binding:"-"`
}

//判断白名单
func (router *Router) checkWhite(c *gin.Context) bool {
	whitelist := strings.Split(router.whiteList, " ")
	clientIp := c.ClientIP()

	if len(whitelist) > 0 {
		for _, v := range whitelist {
			if v == clientIp {
				return true
			}
		}
	} else {
		return true
	}

	return true
}

func (router *Router) Set(engine *gin.Engine) {
	cfg, _ := goconfig.LoadConfigFile(config.CONFIG_FILE_NAME)
	httpCfg, _ := cfg.GetSection("http")
	router.whiteList = httpCfg["white-list"]
	cargameinfo := new(rabgame.CarGameInfo)
	cargameinfo.Init()

	raiseinfo := new(rabgame.RaiseInfo)
	raiseinfo.Init()

	engine.POST("/cargame/insertBetSort", func(c *gin.Context) {
		isWhite := router.checkWhite(c)
		if isWhite {
			var json BetSort
			if err := c.ShouldBindJSON(&json); err != nil {
				c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
				return
			}
			date := time.Now().Format("20060102")
			err := cargameinfo.InsertBetSort(date, json.User, json.Bets)

			if err != nil {
				if strings.Index(err.Error(), "E11000 duplicate key") != -1 {
					uErr := cargameinfo.ModifyBetSort(date, json.User, json.Bets)
					if uErr != nil {
						c.JSON(http.StatusBadRequest, gin.H{"status": "2", "message": uErr})
					} else {
						c.JSON(http.StatusOK, gin.H{"status": "1"})
					}
				} else {
					c.JSON(http.StatusBadRequest, gin.H{"status": "2", "message": err})
				}
			} else {
				c.JSON(http.StatusOK, gin.H{"status": "1"})
			}
		}
	})

	engine.POST("/cargame/getBetSort", func(c *gin.Context) {
		var json BetSort
		if err := c.ShouldBindJSON(&json); err != nil {
			c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
			return
		}

		c.JSON(http.StatusOK, gin.H{"status": "1", "data": cargameinfo.GetBetSort(json.Date)})
	})

	engine.POST("/cargame/insertBet", func(c *gin.Context) {
		isWhite := router.checkWhite(c)
		if isWhite {
			var json CarBet
			if err := c.ShouldBindJSON(&json); err != nil {
				c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
				return
			}

			err := cargameinfo.InsertBet(json.Id, json.Gameid, json.Userid, json.User, json.Digital, json.Quantity, json.Reward, json.Bettime)
			if err != nil {
				if strings.Index(err.Error(), "E11000 duplicate key") != -1 {
					uErr := cargameinfo.ModifyBet(json.Id, json.Gameid, json.Userid, json.User, json.Digital, json.Quantity, json.Reward, json.Bettime)
					if uErr != nil {
						c.JSON(http.StatusBadRequest, gin.H{"status": "2", "message": uErr})
					} else {
						c.JSON(http.StatusOK, gin.H{"status": "1"})
					}
				} else {
					c.JSON(http.StatusBadRequest, gin.H{"status": "2", "message": err})
				}
			} else {
				c.JSON(http.StatusOK, gin.H{"status": "1"})
			}
		}
	})

	engine.POST("/cargame/getBetList", func(c *gin.Context) {
		var json CarBetList
		if err := c.ShouldBindJSON(&json); err != nil {
			c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
			return
		}

		c.JSON(http.StatusOK, gin.H{"status": "1", "data": cargameinfo.GetBetList(json.Gameid, json.User, json.Limit)})
	})

	engine.POST("/cargame/insertGame", func(c *gin.Context) {
		isWhite := router.checkWhite(c)
		if isWhite {
			var json CarGame
			if err := c.ShouldBindJSON(&json); err != nil {
				c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
				return
			}

			err := cargameinfo.InsertGame(json.Gameid, json.Game, json.Number, json.Pool, json.Peoples, json.Createtime)

			if err != nil {
				if strings.Index(err.Error(), "E11000 duplicate key") != -1 {
					uErr := cargameinfo.ModifyGame(json.Gameid, json.Game, json.Number, json.Pool, json.Peoples, json.Createtime)
					if uErr != nil {
						c.JSON(http.StatusOK, gin.H{"status": "2", "message": uErr})
					} else {
						c.JSON(http.StatusOK, gin.H{"status": "1"})
					}
				} else {
					c.JSON(http.StatusOK, gin.H{"status": "2", "message": err})
				}
			} else {
				c.JSON(http.StatusOK, gin.H{"status": "1"})
			}
		}
	})

	engine.POST("/cargame/getGameById", func(c *gin.Context) {
		var json CarGame
		if err := c.ShouldBindJSON(&json); err != nil {
			c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
			return
		}

		c.JSON(http.StatusOK, gin.H{"status": "1", "data": cargameinfo.GetGame(json.Gameid)})
	})

	engine.POST("/cargame/getGameList", func(c *gin.Context) {
		var json CarGameList
		if err := c.ShouldBindJSON(&json); err != nil {
			c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
			return
		}

		c.JSON(http.StatusOK, gin.H{"status": "1", "data": cargameinfo.GetGameList(json.Limit)})
	})
}
